<template>
  <div :class="className" :id="id" :style="{height:height,width:width}" />
</template>

<script type="text/ecmascript-6">
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'bar'
    },
    id: {
      type: String,
      default: 'bar'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        color: ['#3398DB'],
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: '#8492a6',
              opacity: 0.5
            }
          },
          textStyle: {
            fontSize: 12
          }
        },
        grid: {
          top: '5%',
          left: '0%',
          right: '0%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#cccccc'
            }
          },
          axisLabel: {
            color: '#8492a6'
          },
          axisTick: {
            alignWithLabel: true
          },
          data: ['2次以下(不含次)', '2次~3次(不含3次)', '3次~4次(不含4次)', '4次~5次(不含5次)', '5次以上(含5次)']
        }],
        yAxis: [{
          type: 'value',
          max: 100,
          axisLine: {
            show: false,
            lineStyle: {
              color: '#8492a6'
            }
          },
          axisLabel: {
            margin: 10,
            formatter: '{value} %',
            textStyle: {
              fontSize: 12
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              type: 'dotted',
              color: '#d8d8d8'
            }
          }

        }],
        series: [{
          name: '购买频次',
          type: 'bar',
          barWidth: '40%',
          data: [85, 25, 10, 5, 2],
          color: ['#55acee', '#55acee', '#55acee', '#55acee', '#55acee']
        }]

      })
    }
  }
}
</script>